<template>
  <div class="type-box flex-col-start">
    <div class="header">
      <el-dialog :destroy-on-close="true" :visible.sync="showForm" title="新增分类" width="300px">
        <el-form :model="form">
          <el-form-item label="分类名称">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="showForm=false">取 消</el-button>
          <el-button type="primary" @click="handleSure">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="body">
      <el-table
          :data="computedTypeList"
          style="width: 100%">
        <el-table-column
            :index="(index)=>index+1"
            label="序列"
            type="index"
            width="100px"
        >
        </el-table-column>
        <el-table-column
            label="类型名称"
        >
          <template slot-scope="scope">
            <mi-tag :i="scope.$index ">{{ scope.row.name }}</mi-tag>
          </template>

        </el-table-column>
        <el-table-column
            label="创建时间"
        >
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
            align="right">
          <template slot="header" slot-scope="scope">
            <div class="flex-row-start">
              <el-button
                  icon="el-icon-plus"
                  size="mini"
                  style="margin-right: 5px" type="danger" @click="showForm=true">

              </el-button>
              <el-input
                  v-model="search"
                  placeholder="输入关键字搜索"
                  size="mini"/>
            </div>

          </template>
          <template slot-scope="scope">
            <el-button
                icon="el-icon-edit"
                plain
                size="mini"
                type="warning"
                @click="handleEdit(scope.$index, scope.row)">Edit
            </el-button>
            <el-button
                icon="el-icon-delete"
                plain
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="bottom flex-row-center">
      <el-pagination
          :page-size="setting.pageSize"
          :total="setting.total"
          background
          layout="prev, pager, next">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import MiTag from "@/components/label/MiTag.vue";

export default {
  name: "MyType",
  components: {MiTag},
  data() {
    return {
      showForm: false,
      form: {
        name: ""
      },
      setting: {
        pageSize: 10,
        total: 253
      },
      search: "",
      // typeList:[],
      typeList: [{
        createTime: '2016-05-02',
        name: '王小虎',

      }, {
        createTime: '2016-05-04',
        name: '的去武汉',

      }, {
        createTime: '2016-05-01',
        name: '王小虎',

      }, {
        createTime: '2016-05-03',
        name: '和口红管',

      }, {
        createTime: '2016-05-03',
        name: '及格2',

      }, {
        createTime: '2016-05-03',
        name: '王小虎',

      }, {
        createTime: '2016-05-03',
        name: '历史',

      }, {
        createTime: '2016-05-03',
        name: '王小虎',

      }, {
        createTime: '2016-05-03',
        name: '下次',

      }, {
        createTime: '2016-05-03',
        name: '王小虎',

      }, {
        createTime: '2016-05-03',
        name: '王小虎',

      }, {
        createTime: '2016-05-03',
        name: '的撒撒',

      }, {
        createTime: '2016-05-03',
        name: '王小虎',

      }, {
        createTime: '2016-05-03',
        name: '的小虎',

      },
      ],
    }
  },
  methods: {
    handleSure() {
      this.form.name = ""
      this.showForm = false
      this.$message({
        type: 'success',
        message: '添加成功!'
      });
    },
    handleEdit() {

    },
    handleDelete(index, type) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.typeList.splice(index, 1)
        // 确定按钮
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        // 取消按钮
      });
    },
    initTypeList() {
      if (this.typeList.length > this.setting.pageSize) {
        this.typeList = this.typeList.splice(0, this.setting.pageSize)
      }
    }
  },
  computed: {
    computedTypeList() {
      return this.typeList
    }
  },
  created() {
    this.initTypeList()
  }
}
</script>

<style lang="less" scoped>
.type-box {
  width: 100%;
  height: 100%;

  .header {

  }

  .body {
    width: 90%;
    border: 2px solid gainsboro;
    border-radius: 5px;
    padding: 0 20px;
  }

  .bottom {

    padding: 10px 0;
  }
;
}
</style>